package mn.more.wits.client.gui;

import net.mygwt.ui.client.widget.table.Table;
import net.mygwt.ui.client.widget.table.TableColumn;
import net.mygwt.ui.client.widget.table.TableColumnModel;
import net.mygwt.ui.client.widget.table.TableItem;

/**
 * @author Nasanjargal
 * @version $Id: WitsTable.java 5 2008-09-01 12:08:42Z mikeliucc $
 */
public class WitsTable extends Table {

	private static final int DEFAULT_WIDTH = 100;
	private TableColumnModel tcm;
	private TableColumn[] tc;
	//private WitsTable wt;

	public WitsTable(String[] head) {

		//wt = this;
		//Window.addWindowResizeListener(new WindowResizeListener() {
		//    public void onWindowResized(int width, int height) {
		//Timer t = new Timer() {
		//    public void run() {
		//        wt.recalculate();
		//recalculate();
		//}
		//};
		//
		//t.schedule(1000);
		//}
		//});

		tc = new TableColumn[head.length];

		for (int i = 0; i < head.length; i++) {
			tc[i] = new TableColumn(head[i], DEFAULT_WIDTH);
		}

		tcm = new TableColumnModel(tc);

		this.setColumnModel(tcm);

	}

	public WitsTable(String[] head, float[] width) {
		tc = new TableColumn[head.length];

		for (int i = 0; i < head.length; i++) {
			tc[i] = new TableColumn(head[i], width[i]);
		}

		tcm = new TableColumnModel(tc);

		this.setColumnModel(tcm);
	}

	public void deleteRow() {
		TableItem item = this.getSelectedItem();
		this.remove(item);
	}

	public void deleteRow(int rowId) {
		TableItem item = this.getItem(rowId);
		this.remove(item);
	}

	public Object[] getSelectedItems() {
		TableItem item = this.getSelectedItem();
		return item.getValues();
	}

	public void addItem(Object[] items) {
		TableItem row = new TableItem(items);
		this.add(row);
	}

	public TableColumn[] getColumns() {
		TableColumn[] columns = new TableColumn[this.getColumnCount()];

		for (int i = 0; i < columns.length; i++) {
			columns[i] = this.getColumn(i);
		}
		return columns;
	}

	public Object getCellValue(int rowIndex, int columnIndex) {
		return this.getItem(rowIndex).getValue(columnIndex);
	}
}

